#!/bin/sh


APP_HOME=$(cd "$(dirname "$0")"; pwd)
cd ${APP_HOME}

#JDK所在路径  
JAVA_HOME="/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.131-3.b12.el7_3.x86_64"  

SERVER_NAME=${APP_HOME}/mhqx

#Safe close server servlet linsten port
CLOSE_WEB_LISTEN_PORT=`cat ${APP_HOME}/app.properties |grep http.port |awk -F'=' '{print $2}'|sed 's/^[[:space:]]\+//'`
CLOSE_WEB_URL="http://127.0.0.1:${CLOSE_WEB_LISTEN_PORT}/ShutdownServer/stop"

TCP_PORT=`cat ${APP_HOME}/app.properties |grep socket.port |awk -F'=' '{print $2}'|sed 's/^[[:space:]]\+//'`
#java_xmx=`cat ${basedir}/global.config |grep server.memory.size |awk -F'=' '{print $2}'|sed 's/^[[:space:]]\+//'`
java_xmx=512
STDOUT=/dev/null
STDERR=/dev/null
    

#需要启动的Java主程序（main方法类）
CLASS_NAME=org.game.Bootstrap

#pid进程号
PID=${APP_HOME}/app.pid


#CLASS_PATH=./:./config

#for i in "$APP_HOME"/*.jar; do
#   CLASS_PATH="$CLASS_PATH":"$i"
#done


#java虚拟机启动参数 
JAVA_OPTS="$JAVA_OPTS -Dfile.encoding=UTF-8 -Xmx${java_xmx}m -Xms512m -Xmn512m -Xss256k -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=./oom.hprof -XX:+UnlockExperimentalVMOptions -XX:+UseG1GC -XX:MaxGCPauseMillis=50 -XX:GCPauseIntervalMillis=200 -XX:SurvivorRatio=6 -Xloggc:logs/gc.log"


#=============================================================================
start()
{
http_port_check=`netstat -tlpn | grep "\b$CLOSE_WEB_LISTEN_PORT\b"`
tcp_port_check=`netstat -tlpn | grep "\b$TCP_PORT\b"`
if [[  ${http_port_check} != "" ]]
then
	echo
	echo -e "[error]\thttp port:$CLOSE_WEB_LISTEN_PORT is in use"
	echo
	exit 1
elif [[  ${tcp_port_check} != "" ]]
then
	echo
	echo -e "[error]\ttcp port:$TCP_PORT is in use"
	echo
	exit 1 
elif [[ ${java_xmx} == "" ]]
then
	echo
	echo "[error]\t java xmx check false,java xmx is 0"
	echo
	exit 1 
else 
	echo "$CLOSE_WEB_LISTEN_PORT,$TCP_PORT,Port,java xmx ${java_xmx} check OK!"
fi

echo ${JAVA_OPTS}
if test -e $PID
        then
                echo
                echo The $SERVER_NAME Server already Started!
                echo
        else
                echo
                echo Start The $SERVER_NAME Server....
                echo 
		        echo $CLASS_PATH
#                java -Djava.nio.channels.spi.SelectorProvider=sun.nio.ch.EPollSelectorProvider $JAVA_OPTS -classpath $CLASS_PATH $CLASS_NAME >system.out 2>&1 &
                java -Djava.nio.channels.spi.SelectorProvider=sun.nio.ch.EPollSelectorProvider $JAVA_OPTS -jar game.jar >system.out 2>&1 &
                echo $!>$PID
                sleep 2
                STATUS=`ps -p $!|grep java |awk '{print $1}'`
                if test $STATUS
                        then
                                echo The $SERVER_NAME Server Started!
                                echo
                        else
                                rm $PID
				echo The $SERVER_NAME Server Start Failed
                                echo please Check the system
                                echo
                fi
fi
}

stop()
{
if test -e $PID
        then
                TPID=`cat $PID`
                echo

                STATUS=`ps -p $TPID |grep java | awk '{print $1}'`
                if test $STATUS
                then
                        echo Safe Stop The $SERVER_NAME Server [${TPID}]....                    
                else
                        echo "Server is already stoped."
                        rm $PID
                        return
                fi
                
                wget ${CLOSE_WEB_URL} -t 1 -O safe_close.html

                closeResult=`cat safe_close.html`

                echo "wget result : ${closeResult} "

                SUCCESS_TEXT="OK" 

                if [ "$closeResult" = "$SUCCESS_TEXT" ]; then
                    sleep 2
                    while test `ps -p $TPID |grep java | awk '{print $1}'`
                    do
                        echo "waiting for server close proccess job......."
                        sleep 5
                    done
    
                    echo The $SERVER_NAME Server Stoped
                    echo
                    rm $PID
                else
                    echo "close request fail : ${closeResult}"
                fi

                
        else
                echo
                echo The $SERVER_NAME Server already Stoped!
                echo
fi
}


_kill()
{
if test -e $PID
        then
                echo
                echo Kill The $SERVER_NAME Server....
                echo
                TPID=`cat $PID`
                kill -9 $TPID
                sleep 1
                STATUS=`ps -p $TPID |grep java | awk '{print $1}'`
                if test $STATUS
                        then
                                echo The $SERVER_NAME Server NOT Stoped!
                                echo please Check the system
                                echo
                        else
                                echo The $SERVER_NAME Server Stoped
                                echo
                                rm $PID
                fi
        else
                echo
                echo The $SERVER_NAME Server already Stoped!
                echo
fi
}

status()
{
echo
if test -e $PID
        then
                TPID=`cat $PID`
                STATUS=`ps -p $TPID|grep java | awk '{print $1}'`
                if test $STATUS
                        then
                                echo "The $SERVER_NAME Server Running($TPID)!"
                                echo
                        else
                                echo The $SERVER_NAME Server NOT Running!
                                rm $PID
                                echo
                fi
        else
                echo
                echo The $SERVER_NAME Server NOT Running!
                echo
fi
}



case "$1" in
'start')
                start
        ;;
'stop')
                stop
        ;;
'kill')
                _kill
        ;;
'restart')
		stop
		start
	;;
'status')
                status
        ;;
*)
        echo
        echo
        echo "Usage: $0 {status | start | stop | kill}"
        echo
        echo Status of $SERVER_NAME Servers ......
                status
        ;;
esac
exit 0